package me.keiwu.user.service.impl;

import me.keiwu.user.dao.UserStatisticsDao;
import me.keiwu.user.domain.UserStatistics;
import me.keiwu.user.service.UserStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by kei on 07/02/2018.
 */
@Service
@Transactional
public class UserStatisticsServiceImpl implements UserStatisticsService {


    @Autowired
    private UserStatisticsDao userStatisticsDao;

    @Override
    public void incrCommentCount(Long userId) {
        UserStatistics us = userStatisticsDao.findOne(userId);
        if (us == null) {
            us = new UserStatistics();
            us.setUserId(userId);
        }
        us.setCommentCount(us.getCommentCount() + 1);
        userStatisticsDao.save(us);
    }

    @Override
    public void incrStealGoldCount(Long userId) {
        UserStatistics us = userStatisticsDao.findOne(userId);
        if (us == null) {
            us = new UserStatistics();
            us.setUserId(userId);
        }
        us.setStealGoldCount(us.getStealGoldCount() + 1);
        userStatisticsDao.save(us);
    }

    @Override
    public UserStatistics getUserStatistics(Long userId) {
        UserStatistics us = userStatisticsDao.findOne(userId);
        if (us == null) {
            us = new UserStatistics();
            us.setUserId(userId);
        }
        return us;
    }


}
